<?php

	// TITLE: Mortgage AUTO MODEL
	// FILE: scaffolding/model/mortgage.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createMortgage = function($con, $mortgage) {
		$sql = 'INSERT INTO mortgage (id, amount, status, project_id, home_owner_id, bank_id) VALUES (';
		$sql .= $mortgage->getId() . ', ';
		$sql .= $mortgage->getAmount() . ', ';
		$sql .= $mortgage->getStatus() . ', ';
		$sql .= $mortgage->getProjectId() . ', ';
		$sql .= $mortgage->getHomeOwnerId() . ', ';
		$sql .= $mortgage->getBankId() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readMortgage = function($con, $id) {
		$sql = 'SELECT * FROM mortgage WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$mortgage = new Mortgage();
			$mortgage->setId(result[0]);
			$mortgage->setAmount(result[1]);
			$mortgage->setStatus(result[2]);
			$mortgage->setProjectId($dbio->select($readProject(result[3])));
			$mortgage->setHomeOwnerId($dbio->select($readHomeOwner(result[4])));
			$mortgage->setBankId($dbio->select($readBank(result[5])));
		} else {
			$mortgage = false;
		}
		return $mortgage;
	};

	$updateMortgage = function($con, $mortgage) {
		$sql = 'UPDATE mortgage SET ';
		$sql .= 'id = ' . $mortgage->getId() . ', ';
		$sql .= 'amount = ' . $mortgage->getAmount() . ', ';
		$sql .= 'status = ' . $mortgage->getStatus() . ', ';
		$sql .= 'project_id = ' . $mortgage->getProjectId() . ', ';
		$sql .= 'home_owner_id = ' . $mortgage->getHomeOwnerId() . ', ';
		$sql .= 'bank_id = ' . $mortgage->getBankId() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteMortgage = function($con, $id) {
		$sql = 'DELETE FROM mortgage WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listMortgage = function($con) {
		$sql = 'SELECT * FROM mortgage';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$mortgages = array();
			while ($result = mysqli_fetch_array($results)) {
				$mortgage = new Mortgage();
				$mortgage->setId(result[0]);
				$mortgage->setAmount(result[1]);
				$mortgage->setStatus(result[2]);
				$mortgage->setProjectId($dbio->select($readProject(result[3])));
				$mortgage->setHomeOwnerId($dbio->select($readHomeOwner(result[4])));
				$mortgage->setBankId($dbio->select($readBank(result[5])));
				$mortgages[] = mortgage;
			}// end while
		} else {
			$mortgages = false;
		}
		return $mortgages;
	};

